/**
 * 
 */
package ar.unq.edu.svnpermission.homes;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.uqbar.commons.home.db4o.DB4OHomeImpl;
import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.domain.Permission;

/**
 * @author nicolas
 *
 */
public class PermissionHome extends DB4OHomeImpl<Permission> {

	public PermissionHome(Class<Permission> entityType) {
		super(entityType);
	}
	
	/**
	 * 
	 * @param folder
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Permission> getFolderPermissions(Folder folder)
	{
		return (List<Permission>)CollectionUtils.select(this.allInstances(), this.getFolderCriteria(folder));
	}
	
	/**
	 * Obtiene el criterio obtener los Permisos de una carpeta
	 * @param Folder la carpeta para los cuales se quieren obtener los permisos
	 * @return Un predicate
	 */
	private Predicate getFolderCriteria( final Folder folder) {
		return new Predicate() {
			public boolean evaluate(Object arg) {
				Permission permission = (Permission) arg;
				return permission.getFolder().equals(folder);
			}
		};
	}		

}
